/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/8/21 22:13:44
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.UI.LabelProviders;
using LEARNREN.COM.BLL;
using System.Text.RegularExpressions;

namespace LEARNREN.COM.UI
{
    static class LabelContenerHelper
    {
        /// <summary>
        /// 匹配所有的非完全关闭形式的标签。
        /// 形如:{lr_atl:,,}  {lr_atl:,,/}
        /// </summary>
        /// <param name="html"></param>
        /// <param name="groupLogicFactory"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public static List<ILabelProvider> GetLabels(string html, GroupLogicFactory groupLogicFactory, TemplateContext context)
        {
            return GetLabels(html, LabelPattern.SIMPLEPATTERN, groupLogicFactory, context);
        }

        /// <summary>
        /// 根据正则表达式匹配出标签
        /// </summary>
        /// <param name="html">模板的html内容</param>
        /// <param name="pattern">正则表达式</param>
        /// <returns></returns>
        public static List<ILabelProvider> GetLabels(string html, string pattern, GroupLogicFactory groupLogicFactory, TemplateContext context)
        {
            List<string> labels = new List<string>();
            List<ILabelProvider> result = new List<ILabelProvider>();

            Regex r = new Regex(pattern);
            if (r.IsMatch(html))
            {
                foreach (Match item in r.Matches(html))
                {
                    if (!labels.Contains(item.Value))
                    {
                        labels.Add(item.Value);
                    }
                }

                foreach (var labelStr in labels)
                {
                    ILabelProvider label = LabelFactory.Get(labelStr);
                    label.GroupLogicFactory = groupLogicFactory;
                    label.Context = context;
                    result.Add(label);
                }
            }
            return result;
        }
    }
}
